<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'cochera.php';
require_once 'conexion.php';

class PersistenciaCochera {

    private static $instance = false;

    public static function getInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function crearCochera($datos) {
        try {
            $db = Conexion::getInstance();
            $aux = '0';
            if ($this->traerCochera($datos) == null) {
                $sentencia = "INSERT INTO cochera (idcochera,iddepartamento) VALUES('" . $datos[0] . "','" . $datos[1] . "')";
                $db->consulta($sentencia);
                $aux = '1';
            }
            return $aux;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function armarCochera($datos) {
        try {
            require_once 'persistenciaEvento.php';
            $evento = PersistenciaEvento::getInstance();
            $cocheras = array();
            if (!is_null($datos)) {
                foreach ($datos as $cochera) {
                    $coch = new Cochera(null);
                    $coch->idcochera = $cochera[0];
                    $coch->eventos = $evento->traerEvento(1,$cochera[0]);
                    array_push($cocheras, $coch);
                }
            }
            return $cocheras;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function traerCochera($datos) {
        try {
            $db = Conexion::getInstance();
            if (is_array($datos)) {
                $sentencia = "select * from cochera where idcochera='" . $datos[0]."'";
            } else {
                if ($datos == '') {
                    $sentencia = "select * from cochera order by idcochera";
                } else {
                    $sentencia = "select * from cochera where iddepartamento= '" . $datos . "'";
                }
            }
            $aux = $db->consulta($sentencia);
            return $this->armarCochera($aux);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function actualizarCochera($datos) {
        try {
            $db = Conexion::getInstance();
            $sentencia = "UPDATE cochera SET iddepartamento='" . $datos[1] . "' WHERE idcochera='" . $datos[0]."'";
            $db->consulta($sentencia);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    public function eliminarCochera($datos) {
        try {
            $db = Conexion::getInstance();
            $sentencia = "delete from cochera where idcochera='" . $datos."'";
            $db->consulta($sentencia);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

}

?>
